#!/usr/local/bin/python
#-*- encoding:utf-8 -*-

import sys
import utils
from chinesetokenizer import ChineseAnalyzer


analyzer = ChineseAnalyzer();
'''This file is to transform the human written synonym file into index:index file'''
config = utils.get_config();
dictMap = {};
d = open(config.get('rmls', 'dictionary'), 'r');
index  = 0;
for line in d:
    index += 1;
    dictMap[(line.strip('\n').split('\t')[0].decode('utf-8'))] = index;

d.close();
MAX = len(dictMap);
print MAX;

def findIndex(s):
    s = s.decode('utf-8');
    if dictMap.has_key(s):
        return dictMap[s];
    else:
        print 'WRONG', s;
        return -1;

def findPhraseIndex(s):
    di = {}
    tli = analyzer(s.decode('utf-8'));
    for t in tli:
        term = t.text;
        if di.has_key(term):
            di[term] += 1;
        else:
            di[term] = 1;
    res = [];
    for key, value in di.items():
        if dictMap.has_key(key):
            res.append(dictMap[key]);
        else:
            res.append(-1);
    return res;

def main():
    relationalData = open(sys.argv[1], 'r');
    relationData_Index = open(sys.argv[2], 'w');
    for line in relationalData:
        li = line.strip('\n').split();
        i = findPhraseIndex(li[0]);
        j = findIndex(li[1]);
        s = 0;
        for k in i:
            relationData_Index.write('%d\t%d\t%f\n' % (k, j, s));
    relationalData.close();
    relationData_Index.close();
    
if __name__ == '__main__':
    main()
